package model

import (
	"gorm.io/gorm"
	"time"
)

const PredictData_Table = "predict_data"

type PredictData struct {
	gorm.Model
	UserId    uint      `json:"user_id"`
	Value     float64   `json:"value"`
	PredictAt time.Time `json:"predict_at"`
}

func (p *PredictData) Create(db *gorm.DB) error {
	return db.Create(p).Error
}

func (p *PredictData) ToBg() Bg {
	return Bg{
		BgField: BgField{
			Time:  p.PredictAt,
			Value: p.Value,
		},
	}
}

func GetPredicts(db *gorm.DB, userId uint, time Time, number int) []PredictData {
	var predicts []PredictData

	_ = db.Table(PredictData_Table).Limit(number).Where(LtEq("predict_at", time.Time)).Where(Eq("user_id", userId)).Order("predict_at desc").Find(&predicts).Error

	return predicts
}

func GetPredictDataBetween(db *gorm.DB, userId uint, start time.Time, end time.Time) ([]PredictData, error) {
	var datas []PredictData
	err := db.Table(PredictData_Table).Where(GtEq("predict_at", start)).Where(LtEq("predict_at", end)).Order("predict_at desc").Find(&datas).Error
	if err != nil {
		return nil, err
	}
	return datas, nil
}
